//
//  RefreshLineView.m
//  SHShopping
//
//  Created by 坤鼎 on 2019/9/5.
//  Copyright © 2019 kunding. All rights reserved.
//

#import "RefreshLineView.h"

@interface RefreshLineView ()

@property (nonatomic, strong) UIImageView *refreshLineView;

@end

@implementation RefreshLineView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        CGFloat weight = frame.size.height;
        self.layer.cornerRadius = weight / 2;
        self.layer.masksToBounds = YES;
        self.backgroundColor = UIColorFromRGB(0x3170FF);
        
        self.refreshLineView = [[UIImageView alloc] initWithFrame:CGRectMake((weight - 35) / 2, (weight - 35) / 2, 35, 35)];
        self.refreshLineView.image = ImageNamed(@"刷新_line");
        [self addSubview:self.refreshLineView];
        
        
        
        [self addTarget:self action:@selector(actionBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)beginRefresh {
    [self rotateView:self.refreshLineView];
}

- (void)endRefresh {
    [self endView:self.refreshLineView];
    self.refreshLineView.hidden = YES;
}

- (void)actionBtn {
    [self rotateView:self.refreshLineView];
}

- (void)rotateView:(UIImageView *)view {
    [view.layer removeAllAnimations];
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
    rotationAnimation.duration = 1;
    rotationAnimation.repeatCount = HUGE_VALF;
    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

- (void)endView:(UIImageView *)view {
    [view.layer removeAllAnimations];
}


@end
